package com.eascs.admin.sys.permission.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.sys.permission.form.EmployeeForm;
import com.eascs.admin.sys.permission.form.SysUserRoleForm;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.StringUtil;
import com.eascs.site.view.ViewContext;
import com.eascs.web.sso.User;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.entity.sys.ActOrgRoleUserEntity;
import com.eascs.workflow.common.entity.sys.SysRole;
import com.eascs.workflow.common.service.sys.SysPermissionService;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.OrgUserRoleVo;

@Controller
public class UserRoleController extends PermissionBaseMasterViewController{
	@Reference
	private SysPermissionService sysPermissionService;
	
	@RequestMapping("/act/userRole/list")
	public String list(SysUserRoleForm userRoleForm){
		PageResult<OrgUserRoleVo> pageResult = new PageResult<>();
		StringBuffer pageUrl = new StringBuffer("/act/userRole/list?1=1");
		Map<String, String> paramMap = new HashMap<>();
		if(StringUtil.hasText(userRoleForm.getAccount())){
			paramMap.put("account", userRoleForm.getAccount());
			pageUrl.append("&account=").append(userRoleForm.getAccount());
		}
		if(StringUtil.hasText(userRoleForm.getUserName())){
			paramMap.put("userName", userRoleForm.getUserName());
			pageUrl.append("&userName=").append(userRoleForm.getAccount());
		}
		if(StringUtil.hasText(userRoleForm.getOrgId())){
			paramMap.put("orgId", userRoleForm.getOrgId());
			pageUrl.append("&orgId=").append(userRoleForm.getOrgId());
		}
		if(StringUtil.hasText(userRoleForm.getOrgName())){
			paramMap.put("orgName", userRoleForm.getOrgName());
			pageUrl.append("&orgName=").append(userRoleForm.getOrgName());
		}
		ModelResult<PageResult<OrgUserRoleVo>> modelResult = 
				sysPermissionService.searchOrgUserRolePage(paramMap, userRoleForm.getPageSize(), userRoleForm.getPageIndex());
		pageResult = modelResult.getModel();
		ViewContext.put("page", pageResult);
		ViewContext.put("pageUrl", pageUrl.toString());
		ViewContext.put("userRoleForm", userRoleForm);
		return "/sys/userRoleList";
	}
	
	@RequestMapping("/act/employee/list")
	public String employeeList(EmployeeForm employeeForm){
		StringBuffer pageUrl = new StringBuffer("/act/employee/list?1=1");
		PageResult<User> pageResult = new PageResult<>();
		Map<String, String> paramMap = new HashMap<>();
		if(StringUtil.hasText(employeeForm.getEmployeeNamecn())){
			paramMap.put("employeeNamecn", employeeForm.getEmployeeNamecn());
			pageUrl.append("&employeeNamecn=").append(employeeForm.getEmployeeNamecn());
		}
		if(StringUtil.hasText(employeeForm.getEmployeeAccount())){
			paramMap.put("employeeAccount", employeeForm.getEmployeeAccount());
			pageUrl.append("&employeeAccount=").append(employeeForm.getEmployeeAccount());
		}
		ModelResult<PageResult<User>> modelResult = 
				sysPermissionService.searchEmployeePage(paramMap, employeeForm.getPageSize(), employeeForm.getPageIndex());
		pageResult = modelResult.getModel();
		ViewContext.put("page", pageResult);
		ViewContext.put("pageUrl", pageUrl.toString());
		ViewContext.put("employeeForm", employeeForm);
		return "/sys/employeeList";
	}
	
	@RequestMapping("/act/userRole/add-{userNo}")
	public String addAct(@PathVariable String userNo){
		User user = sysPermissionService.getSysUserById(userNo).getModel();
		List<SysRole> roleList = sysPermissionService.getRoleList().getModel();
		//获取全部省区列表
		List<ActOrgEntity> orgList = sysPermissionService.getOrgListByLevel(null, ActOrgEntity.ORG_LEVEL_PROVINCE).getModel();
		//获取全部大区列表
		List<ActOrgEntity> areaList = sysPermissionService.getOrgListByLevel(null, ActOrgEntity.ORG_LEVEL_AREA).getModel();
		ModelResult<List<ActOrgRoleUserEntity>> modelResult = sysPermissionService.getUserRoleList(userNo);
		if(modelResult.isSuccess()){
			ViewContext.put("orgRoleUserList", modelResult.getModel());
		}
		ViewContext.put("roleList", roleList);
		ViewContext.put("orgList", orgList);
		ViewContext.put("areaList", areaList);
		ViewContext.put("user", user);
		return "/sys/addUserRole";
	}
}
